package controller.v2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import model.BoardVO;
import model.LogEarnVO;
import model.MemberMsgVO;
import model.MemberVO;
import model.OrderVO;
import model.ShowwindowCommentVO;
import model.ShowwindowPraiseVO;
import model.ShowwindowVO;
import model.SysMsgVO;
import model.UorderVO;
import service.v2.BoardService;
import service.v2.ConfigService;
import service.v2.MessageService;
import service.v2.MoneyService;
import service.v2.ShowwindowService;
import service.v2.UorderService;
import util.PackageUtil;
import util.PropertieUtil;


@RequestMapping("/v2/msg")
@Controller
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	@Autowired
	private MoneyService moneyService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private BoardService boardService;
	@Autowired
	private ShowwindowService showwindowService;
	@Autowired
	private UorderService uorderService;
	
	/**
	 * 我的资产
	 * @param token 令牌
	 * @param member_id 用户ID
	 * @param page_index
	 */
	@ResponseBody
	@RequestMapping(value="/myAssets",method=RequestMethod.GET) 
	public Object myAssets(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO,LogEarnVO logEarnVO){
		
		Map<String, Object> map = new HashMap<String, Object>();
		//查询我的资产消息列表
		logEarnVO.setLang(lang);
		logEarnVO.setMember_id(member_id);
		int total = moneyService.getMyAssetsTotalById(logEarnVO);
		logEarnVO.setPagination(logEarnVO.getPage_index(),total+"");
		List<LogEarnVO> myAssetsList = moneyService.selectAllMessageById(logEarnVO);
		//更新未读消息状态为已读
		/*if(myAssetsList.size()>0){
			int re= moneyService.updateNoReadMessages(myAssetsList);
		}*/
		messageService.resetMemberMsg(member_id, "4");
		map.put("pagination",logEarnVO);
		map.put("myAssetsList",myAssetsList);
		return PackageUtil.packageParams(200,"successs",map,"");
	}
	
	
	@ResponseBody
	@RequestMapping(value="/deliverAssist",method=RequestMethod.GET) 
	public Object deliverAssist(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO){
		List<UorderVO> deliverList = uorderService.getDeliverAssist(member_id);
		return PackageUtil.packageParams(200,"successs",deliverList,"");
	}
	/**
	 * 消息>社区分享>种草社区
	 * @param token 令牌
	 * @param member_id 用户ID
	 */
	@ResponseBody
	@RequestMapping(value="/communityShare",method=RequestMethod.GET) 
	public Object communityShare(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO,ShowwindowVO showwindowVO,ShowwindowCommentVO showwindowCommentVO, ShowwindowPraiseVO praiseVO){
		Map<String, Object> map = new HashMap<String, Object>();
		/*Map<String, Object> map = new HashMap<String, Object>();
		//获取消息页面数量
		MemberMsgVO memberMsg = messageService.getMemberMsg(member_id);
		//点赞数
		int dz_num = Integer.parseInt(memberMsg.getPraise_num());
		//评论数
		showwindowCommentVO.setCreate_by(member_id);
		showwindowCommentVO.setRead_yn("N");
		int pl_num = showwindowService.getReplyTotalByMember(showwindowCommentVO);
		
		map.put("dz_num",dz_num+"");
		map.put("pl_num",pl_num+"");*/
		showwindowCommentVO = new ShowwindowCommentVO();
		showwindowCommentVO.setCreate_by(member_id);
		showwindowCommentVO.setRead_yn("N");
		int pl_num = showwindowService.getReplyTotalByMember(showwindowCommentVO);
		praiseVO.setMember_id(member_id);
		praiseVO.setRead_yn("N");
		int praise_num = showwindowService.getPraiseTotalByMember(praiseVO);
		map.put("dz_num", praise_num);
		map.put("pl_num", pl_num);
		return PackageUtil.packageParams(200,"successs",map,"");
	}
	
	/*
	 * 系统通知
	 * 
	 */
	@ResponseBody
	@RequestMapping(value="/sysNotice",method=RequestMethod.GET) 
	public Object sysNotice(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO){
		if(member_id==null||"".equals(member_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		
		OrderVO order = new OrderVO();
		order.setCreate_by(member_id);
		int total = messageService.getCancelOrderToTal(order);
		order.setPagination(order.getPage_index(), total + "");
		//后台取消订单
		List<UorderVO> orderList = messageService.getCancelOrder(order);
		Map map = new HashMap();
		map.put("pagination", order);
		map.put("list", orderList==null?new ArrayList<OrderVO>():orderList);
		messageService.resetMemberMsg(member_id, "2");
		return PackageUtil.packageParams(200,"successs",map,"");
	}
	
	/*
	 * 新版系统通知
	 * 
	 */
	@ResponseBody
	@RequestMapping(value="/sysMsg",method=RequestMethod.GET) 
	public Object sysMsg(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO) throws Exception{
		if(member_id==null||"".equals(member_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		SysMsgVO sys = new SysMsgVO();
		sys.setMember_id(member_id);
		int total = messageService.getSysMsgTotal(sys);
		sys.setPagination(sys.getPage_index(), total + "");
		List<SysMsgVO> sysList = messageService.SysMsg(sys);
		Map map = new HashMap();
		map.put("pagination", sys);
		map.put("list", sysList);
		String center_url =host+version+"/member/memberAward";
		String level_up_url = host+version+"/member/levelupForward";
		map.put("center_url", center_url);
		map.put("level_up_url", level_up_url);
		messageService.resetMemberMsg(member_id, "2");
		return PackageUtil.packageParams(200,"successs",map,"");
	}
	
	/*
	 * 新版消息
	 * 
	 * 
	 */
	@ResponseBody
	@RequestMapping(value="/messageBox",method=RequestMethod.GET) 
	public Object messageBox(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,
			MemberVO memberVO,LogEarnVO logEarnVO, ShowwindowPraiseVO praiseVO ) throws Exception{
		if(member_id==null||"".equals(member_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		Map<String,Object> map  = new HashMap<String,Object>();
		//获取消息页面数量
		MemberMsgVO memberMsg = messageService.getMemberMsg(member_id);
		
		//_____________________________________________________________________________________________________________
		//友米头条
		String last_notice_title = messageService.getLastNoticeTitle(PropertieUtil.loadPropertie("app_notice"));
		map.put("last_notice_title", last_notice_title);
		map.put("notice_num", memberMsg.getNotice_num());
		//_____________________________________________________________________________________________________________
		
		//系统通知
		//String  order_no = messageService.getLastCancelOrder(member_id);
		String sys_msg = messageService.getLastSysMsg(member_id);
		map.put("sys_msg", sys_msg==null?"":sys_msg);
		map.put("sys_num", memberMsg.getSys_num());
		//_____________________________________________________________________________________________________________
		
		//物流助手
		String ordNo = messageService.getSendOrder(member_id);
		map.put("ordNo", ordNo==null?"":ordNo);
		map.put("ord_Read_N", memberMsg.getEms_num());
		//_____________________________________________________________________________________________________________
		
		// 我的资产>显示最新一笔收入
		LogEarnVO latest_income = messageService.selectLatestIncome(member_id,logEarnVO);
		map.put("latest_income", latest_income==null?"":latest_income.getEarn_money());
		map.put("myAssets_read_n", memberMsg.getCash_num());		
		//_____________________________________________________________________________________________________________
				
		//社区分享
		ShowwindowCommentVO showwindowCommentVO = new ShowwindowCommentVO();
		showwindowCommentVO.setCreate_by(member_id);
		showwindowCommentVO.setRead_yn("N");
		int pl_num = showwindowService.getReplyTotalByMember(showwindowCommentVO);
		praiseVO.setMember_id(member_id);
		praiseVO.setRead_yn("N");
		int praise_num = showwindowService.getPraiseTotalByMember(praiseVO);
		String community_msg = showwindowService.getcommunityMsg(member_id);
		String community_share_num = String.valueOf(pl_num + praise_num);
		map.put("community_msg",community_msg);//文字
		map.put("community_share_num", community_share_num);//数量
		
		//_____________________________________________________________________________________________________________
		
		return PackageUtil.packageParams(200,"successs",map,"");
	}
	
 
}
